<template>
  <div>
    <header>
      <el-form inline @submit.native.prevent ref="searchForm" :model="form">
        <el-form-item label="企业名称" prop="uscCode" class="base-search__form-item">
          <el-select v-model="form.uscCode" @change="selectEnterprise" filterable>
            <el-option
              v-for="(item, index) in enterpriseList"
              :key="index"
              :label="item.tenantName"
              :value="item.uscCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="业务类型" prop="type" class="base-search__form-item">
          <el-select v-model="form.type" @change="selectType">
            <el-option
              v-for="(item, index) in typeList"
              :key="index"
              :label="item.label"
              :value="item.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="更新时间" prop="searchTime" class="base-search__form-item">
          <el-select v-model="form.searchTime">
            <el-option v-for="(item, index) in timeList" :key="index" :label="item" :value="item"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item class="common-search__footer">
          <span @click="reset">重置</span>
          <span @click="submit">搜索</span>
        </el-form-item>
      </el-form>
    </header>
    <nav class="common-nav mb20" v-if="isInit">
      <h2
        style="width: 120px"
        class="common-nav__title"
        :class="item.id === activeIndex ? 'tab_active' : ''"
        @click="switchNav(item)"
        v-for="(item, index) in navList"
        :key="index"
      >
        {{ item.name }}
      </h2>
    </nav>
    <Empty v-if="!isInit"></Empty>
    <!-- 核心风控数据 -->
    <section v-if="isInit && activeIndex === 1">
      <CompanyInfo :baseInfo="baseInfo" :isCredit="isCredit"></CompanyInfo>
      <RebateRegionCount :rebateRegionCount="rebateRegionCount"></RebateRegionCount>
      <RebateCommodityCount :rebateCommodityCount="rebateCommodityCount"></RebateCommodityCount>
      <DrawbackInfo v-if="isCredit" :drawbackInfo="drawbackInfo"></DrawbackInfo>
    </section>
    <!-- 企业基础信息 -->
    <section v-if="isInit && activeIndex === 2">
      <BaseInfo :baseInfo="companyInfo"></BaseInfo>
    </section>
    <!-- 历史退税数据 -->
    <DataCenterDetail v-if="isInit && !isCredit && activeIndex === 3" :data="allDrawbackInfo"></DataCenterDetail>
    <!-- 纳税申报表 -->
    <TaxDeclaration
      v-if="isInit && activeIndex === 4"
      :taxDeclaration="taxDeclaration"
      :showTime="TaxDeclarationTime"
    ></TaxDeclaration>
    <!-- 资产负债表 -->
    <AssetsLiabilitiesInfo
      :showTime="assetsLiabilitiesInfoTime"
      :assetsLiabilitiesInfo="assetsLiabilitiesInfo"
      v-if="isInit && activeIndex === 5"
    ></AssetsLiabilitiesInfo>
    <!-- 利润表 -->
    <Profit v-if="isInit && activeIndex === 6" :showTime="profitTime" :profit="profit"></Profit>
  </div>
</template>

<script>
import Empty from '@/components/Empty/index.vue'
import CompanyInfo from '@/components/RiskControl/CompanyInfo/index.vue'
import RebateRegionCount from '@/components/RiskControl/RebateRegionCount/index.vue'
import RebateCommodityCount from '@/components/RiskControl/RebateCommodityCount/index.vue'
import DrawbackInfo from '@/components/RiskControl/DrawbackInfo/index.vue'
import BaseInfo from '@/components/RiskControl/BaseInfo/index.vue'
import TaxDeclaration from '@/components/RiskControl/TaxDeclaration/index.vue'
import AssetsLiabilitiesInfo from '@/components/RiskControl/AssetsLiabilitiesInfo/index.vue'
import Profit from '@/components/RiskControl/Profit/index.vue'
import { rebateTypeObj, taxPayerTypeObj, logoutTypeObj } from '@/constants/riskControlModel.js'
import DataCenterDetail from '@/components/DataCenter/index.vue'

export default {
  name: 'wmrDataList',
  components: {
    Empty,
    CompanyInfo,
    RebateRegionCount,
    RebateCommodityCount,
    DrawbackInfo,
    BaseInfo,
    TaxDeclaration,
    AssetsLiabilitiesInfo,
    Profit,
    DataCenterDetail,
  },
  data() {
    return {
      isInit: 0,
      activeIndex: 1,
      isCredit: true,
      navList: [
        // { name: '历史退税数据', id: 3 }, // 贷中(企业详情)没有历史退税数据，准入(授信)无数据隐藏
        // { name: '纳税申报表', id: 4 }, // 无数据隐藏
        // { name: '资产负债表', id: 5 }, // 无数据隐藏
        // { name: '利润表', id: 6 }, // 无数据隐藏
      ],
      enterpriseList: [],
      typeList: [
        { label: '准入', value: 2 },
        { label: '贷中', value: 1 },
      ],
      timeList: [],
      form: {
        uscCode: '',
        type: '',
        searchTime: '',
      },
      table: {
        data: [],
      },
      // 基础信息
      baseInfo: {},
      // 退税项下关单贸易国数据统计(近1年Top5数据)
      rebateRegionCount: [],
      // 退税项下关单商品类别数据统计(近1年top5数据)
      rebateCommodityCount: [],
      // 上月退税数据
      drawbackInfo: [],
      // 企业基础信息
      companyInfo: [],
      // 历史退税数据
      allDrawbackInfo: [],
      // 纳税申报表
      taxDeclaration: {},
      // 纳税申报时间区间
      TaxDeclarationTime: {
        taxPeriodStart: '',
        taxPeriodEnd: '',
      },
      // 资产负债表
      assetsLiabilitiesInfo: [],
      // 资产负债时间
      assetsLiabilitiesInfoTime: { assetsPeriodStart: '', assetsPeriodEnd: '' },
      // 利润表
      profit: [],
      // 利润时间区间
      profitTime: { profitPeriodStart: '', profitPeriodEnd: '' },
    }
  },
  methods: {
    selectEnterprise(val) {
      if (this.form.type) {
        this.getTimeRequest()
      }
    },
    selectType(type) {
      if (this.form.uscCode) {
        this.getTimeRequest()
      }
    },
    switchNav(item) {
      if (this.activeIndex === item.id) return
      this.activeIndex = item.id
    },
    submit() {
      const emptyLen = Object.values(this.form).filter(item => !item).length
      if (!emptyLen) {
        this.isInit++
        this.resolveNavList(this.form.type)
        this.getListRequest()
      } else {
        this.$message.warning('请输入搜索条件')
      }
    },
    reset() {
      this.$refs.searchForm.resetFields()
    },
    resolveNavList(type) {
      const inCredit = 1 // 同企业详情
      const unCredit = 2 // 同授信
      if (type === inCredit) {
        this.navList = [
          { name: '核心风控数据', id: 1 },
          { name: '企业基础信息', id: 2 },
        ]
        this.isCredit = true
        return
      }
      if (type === unCredit) {
        this.navList = [
          { name: '核心风控数据', id: 1 },
          { name: '企业基础信息', id: 2 },
          // { name: '历史退税数据', id: 3 },
        ]
        this.isCredit = false
        return
      }
    },
    async getListRequest() {
      const {
        companyInfo,
        allDrawbackInfo,
        rebateRegionCount,
        rebateCommodityCount,
        drawbackInfo,
        taxDeclaration,
        assetsLiabilitiesInfo,
        profit,
        taxPeriodStart,
        taxPeriodEnd,
        profitPeriodStart,
        profitPeriodEnd,
        assetsPeriodStart,
        assetsPeriodEnd,
      } = await this.$http.get('/data_center/risk_info', {
        params: {
          ...this.form,
        },
      })
      this.baseInfo = companyInfo
      this.rebateRegionCount = rebateRegionCount
      this.rebateCommodityCount = rebateCommodityCount
      this.drawbackInfo = drawbackInfo
      // 企业基础信息
      this.resolveCompanyInfo(companyInfo)
      // 历史退税数据
      if (!this.isCredit) {
        this.resolveAllDrawbackInfo(allDrawbackInfo)
      }
      // 纳税申报表
      this.resolveTaxDeclaration(taxDeclaration)
      // 资产负债表
      this.resolveAssetsLiabilitiesInfo(assetsLiabilitiesInfo)
      // 利润表
      this.resolveProfitData(profit)
      this.TaxDeclarationTime.taxPeriodStart = taxPeriodStart
      this.TaxDeclarationTime.taxPeriodEnd = taxPeriodEnd
      this.profitTime.profitPeriodStart = profitPeriodStart
      this.profitTime.profitPeriodEnd = profitPeriodEnd
      this.assetsLiabilitiesInfoTime.assetsPeriodStart = assetsPeriodStart
      this.assetsLiabilitiesInfoTime.assetsPeriodEnd = assetsPeriodEnd
    },
    resolveCompanyInfo(companyInfo) {
      this.companyInfo = [
        {
          rightTitle: '企业名称',
          rightValue: companyInfo.tenantName,
          leftTitle: '社会信用码',
          leftValue: companyInfo.uscCode,
        },
        {
          rightTitle: '联系电话',
          rightValue: companyInfo.telephone,
          leftTitle: '电子邮箱',
          leftValue: companyInfo.postalCode,
        },
        {
          rightTitle: '注册地址',
          rightValue: companyInfo.registeredAddress,
          leftTitle: '生产经营地址',
          leftValue: companyInfo.productionSite,
        },
        {
          rightTitle: '法定代表人',
          rightValue: companyInfo.legalRepresentative,
          leftTitle: '法定代表人电话',
          leftValue: companyInfo.legalRepresentativeTelephone,
        },
        {
          rightTitle: '法定代表人身份证',
          rightValue: companyInfo.legalRepresentativeNo,
          leftTitle: '开业日期',
          leftValue: companyInfo.openingTime,
        },
        {
          rightTitle: '注册资金',
          rightValue: companyInfo.registeredCapital,
          leftTitle: '经营期限(年)',
          leftValue:
            companyInfo.operationStart &&
            companyInfo.operationStart + '-' + companyInfo.operationEnd &&
            companyInfo.operationEnd,
        },
        {
          rightTitle: '是否注销',
          rightValue: logoutTypeObj[companyInfo.logoutType] || '--',
          leftTitle: '注销日期',
          leftValue: companyInfo.logoutTime,
        },
        {
          rightTitle: '主管税务机关',
          rightValue: companyInfo.taxAuthorityName,
          leftTitle: '经营者类型',
          leftValue: '--',
        },
        {
          rightTitle: '企业类型',
          rightValue: companyInfo.registerType,
          leftTitle: '登记注册类型',
          leftValue: companyInfo.registerType,
        },

        {
          rightTitle: '所属行业',
          rightValue: companyInfo.tradeCode,
          leftTitle: '纳税人类型',
          leftValue: taxPayerTypeObj[companyInfo.taxPayerType] || '--',
        },
        {
          rightTitle: '隶属关系',
          rightValue: companyInfo.subjectionType,
          leftTitle: '纳税信用等级',
          leftValue: companyInfo.creditRating,
        },
        { rightTitle: '是否零税率', rightValue: companyInfo.zeroTaxRate, leftTitle: '工商登记号', leftValue: '--' },
        {
          rightTitle: '退税计算方式',
          rightValue: rebateTypeObj[companyInfo.rebateType] || '--',
          leftTitle: '财务负责人',
          leftValue: companyInfo.financialOfficer,
        },
        {
          rightTitle: '财务负责人电话',
          rightValue: companyInfo.financialOfficerPhone,
          leftTitle: '财务负责人身份证号',
          leftValue: companyInfo.financialOfficernNo,
        },
        {
          rightTitle: '分类等级管理',
          rightValue: companyInfo.managementLevel,
          leftTitle: '出口备案日期',
          leftValue: companyInfo.exportRecordTime,
        },
        {
          rightTitle: '首次申报年月',
          rightValue: companyInfo.firstDeclarationYear,
          leftTitle: '累计退税申报次数',
          leftValue: companyInfo.declareNumber,
        },
      ]
    },
    resolveAllDrawbackInfo(array) {
      if (array && !array.length) {
        if (this.navList.findIndex(item => item.id === 3) !== -1) {
          this.navList.splice(
            this.navList.findIndex(item => item.id === 3),
            1
          )
        }
        this.allDrawbackInfo = []
        this.activeIndex = 1
      } else {
        if (!this.navList.find(item => item.id === 3)) {
          this.navList.push({ name: '历史退税数据', id: 3 })
        }
        this.allDrawbackInfo = array
      }
    },
    resolveTaxDeclaration(obj) {
      if (!obj.generalMonth && !obj.generalTotal && !obj.refundMonth && !obj.refundTotal) {
        if (this.navList.findIndex(item => item.id === 4) !== -1) {
          this.navList.splice(
            this.navList.findIndex(item => item.id === 4),
            1
          )
        }
        this.taxDeclaration = {}
        this.activeIndex = 1
      } else {
        if (!this.navList.find(item => item.id === 4)) {
          this.navList.push({ name: '纳税申报表', id: 4 })
        }
        this.taxDeclaration = obj
      }
    },
    // 资产负债表数据处理
    resolveAssetsLiabilitiesInfo(array) {
      if (array && !array.length) {
        if (this.navList.findIndex(item => item.id === 5) !== -1) {
          this.navList.splice(
            this.navList.findIndex(item => item.id === 4),
            1
          )
        }
        this.assetsLiabilitiesInfo = []
        this.activeIndex = 1
      } else {
        if (!this.navList.find(item => item.id === 5)) {
          this.navList.push({ name: '资产负债表', id: 5 })
        }
        this.assetsLiabilitiesInfo = [
          {
            item: '行次',
            assetName: '资产',
            assetEndBalance: '期末余额',
            assetStartBalance: '年初余额',
            equityName: '负债和所有者权益(或股东权益)',
            equityEndBalance: '期末余额',
            equityStartBalance: '年初余额',
          },
          ...array,
        ]
      }
    },
    // 利润表数据处理
    resolveProfitData(array) {
      if (array && !array.length) {
        if (this.navList.findIndex(item => item.id === 6) !== -1) {
          this.navList.splice(
            this.navList.findIndex(item => item.id === 6),
            1
          )
        }
        this.profit = []
        this.activeIndex = 1
      } else {
        if (!this.navList.find(item => item.id === 6)) {
          this.navList.push({ name: '利润表', id: 6 })
        }
        this.profit = [
          { item: '行次', projectName: '项目', currentAmount: '本月数', cumulativeAmount: '本年累计数' },
          ...array,
        ]
      }
    },
    async getTimeRequest() {
      const res = await this.$http.get('/data_center/risk_info/date_list', {
        params: {
          type: this.form.type,
          uscCode: this.form.uscCode,
        },
        isLoading: false,
      })

      this.timeList = res
      this.form.searchTime = this.timeList.length ? this.timeList[0] : ''
    },
    async getEnterpriseListRequest() {
      const res = await this.$http.get('/ents/list', { isLoading: false })
      this.enterpriseList = res
    },
  },
  mounted() {
    this.getEnterpriseListRequest()
  },
}
</script>

<style lang="scss" scoped>
.common-search__footer {
  span {
    display: inline-block;
    text-align: center;
    border-radius: 1px;
    width: 136px;
    height: 36px;
    line-height: 36px;
    cursor: pointer;
    &:first-child {
      border: 1px solid $blue;
      color: $blue;
      margin-right: 10px;
    }
    &:last-child {
      background: $blue;
      color: $white;
      border: 1px solid transparent;
    }
  }
}
::v-deep {
  .base-search__form-item {
    margin-right: 40px;
  }
  .el-input {
    width: 260px;
  }
}
</style>
